package com.bjtech.dao.ibatis;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.ssi.dao.impl.BaseDaoSqlMapImpl;

import com.bjtech.bo.RoomImportDataBO;
import com.bjtech.dao.BaseRoomInfoDao;
import com.bjtech.domain.AssignMeterInfo;
import com.bjtech.domain.BaseBuildingInfo;
import com.bjtech.domain.BaseRoomInfo;
import com.bjtech.domain.BaseUnitInfo;
import com.bjtech.domain.LoginUserInfo;
import com.bjtech.domain.SinkInfo;

/**
 * @author wf
 *
 */
@Repository(value="baseRoomInfoDao")
public class SqlMapBaseRoomInfoDao extends BaseDaoSqlMapImpl<BaseRoomInfo> implements
		BaseRoomInfoDao {

	public List selectRoomListByUnitId(Long unitId) {
		return super.getSqlMapClientTemplate().queryForList("selectRoomListByUnitId", unitId);
	}

	public List selectRoomListByBuildId(Long buidlId) {
		return super.getSqlMapClientTemplate().queryForList("selectRoomListByBuildId", buidlId);
	}

	public String insertUploadRoomInfos(List<RoomImportDataBO> records) {
		//先遍历是否有重复的分配表
		for(RoomImportDataBO dataBO:records){
			for(int i=0;i<dataBO.getSinkInfoList().size();i++){
				AssignMeterInfo meter = dataBO.getAssignMeterList().get(i);
				if(!"".equals(meter.getSerious_num())&&meter.getSerious_num()!=null){
					Long isExit = (Long) super.getSqlMapClientTemplate().queryForObject("selectAssignMeterInfoCountBySeriousNum",meter.getSerious_num());
					if(isExit>0){//存在重复的序列号，然后跳过
						return "Excel中存在重复信息，无法导入！";
					}
				}
			}
		}
		for(RoomImportDataBO dataBO:records){
			if(dataBO.getBuild_no()!=null&&!"".equals(dataBO.getBuild_no())){
				BaseBuildingInfo build = (BaseBuildingInfo) super.getSqlMapClientTemplate().queryForObject("selectBuildInfoByBuildNo",dataBO.getBuild_no());
				if(build!=null&&build.getId()!=null){
					BaseUnitInfo unit = dataBO.getBaseUnitInfo();
					unit.setBuild_id(build.getId());
					Long unit_id;
					BaseUnitInfo temp_unit = (BaseUnitInfo) super.getSqlMapClientTemplate().queryForObject("selectUnitInfoByBuildIdUnitNo",unit);
					if(temp_unit!=null){
						unit_id = temp_unit.getId();
					}else{
						unit_id = (Long) super.getSqlMapClientTemplate().insert("insertBaseUnitInfo",unit);
					}
					BaseRoomInfo room = dataBO.getBaseRoomInfo();
					room.setProj_id(build.getProj_id());
					room.setBuild_id(build.getId());
					room.setArea_id(build.getArea_id());
					room.setUnit_no(unit_id);
					Long room_id = super.insertEntity(room);
					//导入登陆用户信息
					LoginUserInfo loginUser = new LoginUserInfo();
					loginUser.setUser_name(room.getName()+room.getHouse_num());
					loginUser.setPass_word("123456");
					loginUser.setUser_role(7l);
					loginUser.setRoom_id(room_id);
					loginUser.setProj_id(room.getProj_id());
					loginUser.setBuild_id(room.getBuild_id());
					loginUser.setShare_area_id(room.getArea_id());
					loginUser.setReal_name(room.getName());
					super.getSqlMapClientTemplate().insert("insertLoginUserInfo",loginUser);
					
					for(int i=0;i<dataBO.getSinkInfoList().size();i++){
						SinkInfo sink = dataBO.getSinkInfoList().get(i);
						sink.setArea_id(build.getArea_id());
						sink.setBuild_id(build.getId());
						sink.setRoom_id(room_id);
						if(sink.getPiece_num()!=null&&sink.getCal_power()!=null&&sink.getCouple_factor()!=null&&sink.getOther_factor()!=null){
							sink.setPower_factor(new BigDecimal(sink.getPiece_num()).multiply(sink.getCal_power()).multiply(sink.getCouple_factor()).multiply(sink.getOther_factor()).multiply(room.getXz_factor()));
						}
						
						Long sink_id = (Long) super.getSqlMapClientTemplate().insert("insertSinkInfo",sink);
						AssignMeterInfo meter = dataBO.getAssignMeterList().get(i);
						//当分配表编号为空时，不导入分配表信息
						if(StringUtils.isNotBlank(meter.getSerious_num())){
							meter.setArea_id(build.getArea_id());
							meter.setBuild_id(build.getId());
							meter.setRoom_id(room_id);
							meter.setSink_id(sink_id);
							super.getSqlMapClientTemplate().insert("insertAssignMeterInfo",meter);
						}
					}
				}
			}
		}
		return "";
	}

	public void deleteRoomLinkedObjs(Long id) {
		//级联删除room对应的登陆用户信息
		super.getSqlMapClientTemplate().delete("deleteLoginUserByRoomId",id);
		
		super.getSqlMapClientTemplate().delete("deleteAssignMeterInfoByRoomId",id);
		super.getSqlMapClientTemplate().delete("deleteSinkInfoByRoomId",id);
		
	}

	public BaseRoomInfo selectBaseRoomInfoByAssignMeterNum(String seriousNum) {
		return (BaseRoomInfo)super.getSqlMapClientTemplate().queryForObject("selectBaseRoomInfoByAssignMeterNum",seriousNum);
	}

}
